package databaseproject;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Scanner;

/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
/**
 *
 * @author chris
 */
public class Menu {

    private ArrayList<String> optionList;

    private String title;
    private boolean hasQuitOption;
    private boolean hasBackOption;
    
    private int backOptionPosition = 0;
    private int quitOptionPosition = 0;

    public Menu()
    {
        optionList = new ArrayList<String>();
        hasBackOption = true;
    }
    
    public void addOption(String text)
    {
        optionList.add(text);
    }
    
    public void addOptionsFromSQLQuery(Connection conn, String qry)
    {
        try
        {
            Statement stmt = conn.createStatement(
                                        ResultSet.TYPE_SCROLL_INSENSITIVE,
                                        ResultSet.CONCUR_READ_ONLY);
            ResultSet rs = stmt.executeQuery(qry);


            if(rs.first()){
                do
                {
                    addOption(rs.getString(1));
                } while (rs.next());
            }

        }
        catch (java.sql.SQLException e)
        {
            System.out.printf("Error in SQL statement [%s].\n", qry);
            return;
        }
    }
    
    public void addTwoOptionsFromSQLQuery(Connection conn, String qry)
    {
        try
        {
            Statement stmt = conn.createStatement(
                                        ResultSet.TYPE_SCROLL_INSENSITIVE,
                                        ResultSet.CONCUR_READ_ONLY);
            ResultSet rs = stmt.executeQuery(qry);


            if(rs.first()){
                do
                {
                    addOption(rs.getString(1)+ " " + rs.getString(2));
                } while (rs.next());
            }

        }
        catch (java.sql.SQLException e)
        {
            System.out.printf("Error in SQL statement [%s].\n", qry);
            return;
        }
    }
    
    public void addQuitOption()
    {
        hasQuitOption = true;
    }
    
    public void removeBackOption()
    {
        hasBackOption = false;
    }
    
    public String getOptionAt(int i) {
        return optionList.get(i-1);
    }
    
    public int getBackOption()
    {
        return backOptionPosition + 1; // returns a 1-indexed position
    }
    public int getQuitOption()
    {
        return quitOptionPosition + 1; // returns a 1-indexed position
    }
    
    public int display()
    {
        int i;
        int selection = 0;
        Scanner scanner;
        
        if (hasBackOption && backOptionPosition == 0)
        {
            backOptionPosition = optionList.size();
            addOption("  Back");
        }
        if (hasQuitOption && quitOptionPosition == 0)
        {
            quitOptionPosition = optionList.size();
            addOption("  Quit");
        }
        System.out.println();
        if (title != null)
            System.out.println(title);
        
        for (i = 0; i < optionList.size(); i++)
        {
            System.out.printf("%3d - %s\n", i+1, optionList.get(i));
        }
        
        
        scanner = new Scanner(System.in);
        do
        {
            System.out.print("Choice: ");
            
            String temp = scanner.next();
            try{
            selection = Integer.parseInt(temp);
            }
            catch(Exception e){
                System.out.println("Invalid Entry");
            }
        } while (selection < 1 || selection > optionList.size());
        
        return selection;
                    
    }
    
    public void setTitle(String title)
    {
        this.title = title;
    }
}
